package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.EntryBean;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.converter.CountryConverter;
import com.wego.converter.ProvinceConverter;
import com.wego.converter.trans.CountryTrans;
import com.wego.entity.domain.City;
import com.wego.entity.domain.Country;
import com.wego.entity.domain.Province;
import com.wego.entity.query.CountryQuery;
import com.wego.entity.vo.manager.CountryVO;
import com.wego.service.CityService;
import com.wego.service.CountryService;
import com.wego.service.ProvinceService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 县区控制器
 *
 * @author hc
 */
@Controller
@RequestMapping("/manager/country")
public class CountryController extends BaseController<Country> {
    @Autowired
    private CountryService countryService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private ProvinceConverter provinceConverter;

    @Autowired
    private CountryConverter countryConverter;

    @Autowired
    private CountryTrans countryTrans;

    /**
     * 不提供默认的构造方法，限制其子类必须调用该方法为baseService赋值
     * @param service
     */
    public CountryController(BaseService<Country> service) {
        super(service);
    }

    /**
     * 省份列表
     *
     * @param countryQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(CountryQuery countryQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String keywords = countryQuery.getKeywords();
        if (keywords != null) {
            if (countryQuery.getName() == null) {
                countryQuery.setName(keywords);
            }
        }

        PageBean<Country> countryPageBean = countryService.selectPage(countryQuery);
        //转换
        PageBean<CountryVO> pageBean = countryTrans.getInstance().tPageBean2VPageBean(countryPageBean);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(countryQuery);
        pageBean.setUrl("manager/country/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("countryQuery", countryQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/country";
    }


    /**
     * 打开添加修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @Override
    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model) {
        final List<Province> provinceList = provinceService.selectAll();
        final List<EntryBean<String>> entryBeanList = provinceConverter.provinceList2EntryList(provinceList);
        model.addAttribute("provinceList", entryBeanList);

        if (id != null) {
            Country country = countryService.selectByPrimaryKey(id);
            model.addAttribute("country", country);

            if (country != null) {
                final City city = cityService.selectByPrimaryKey(country.getCityId());
                if (city != null) {
                    model.addAttribute("city", city);
                    final Province province = provinceService.selectByPrimaryKey(city.getProvinceId());
                    model.addAttribute("province", province);
                }
            }
        }

        return "manager/country_add_update";
    }

    /**
     * 具体执行添加修改操作
     *
     * @param country
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Country country, RedirectAttributes redirectAttributes) {
        country.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = country.getId();
        if (id != null) {
            //修改
            boolean res = countryService.updateByPrimaryKeySelective(country);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            country.setCreateTime(LocalDateTime.now());
            boolean res = countryService.insertSelective(country);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }
}
